{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# process data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\tqdm\\auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import cv2\n",
    "import numpy as np\n",
    "import os\n",
    "import torch\n",
    "from my_py_toolkit.cv.blend_with_landmark import self_blend, get_trans\n",
    "from my_py_toolkit.file.file_toolkit import *\n",
    "from torch.utils.data import DataLoader, Dataset\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = 'F:/VOS/ubutu-vm/resources/datasets/work/ps dataset'\n",
    "data_dir = f'{root_dir}/train'\n",
    "landmarks_path = f'{root_dir}/landmarks.json'\n",
    "batch_size = 2\n",
    "img_size = (240, 240)\n",
    "crop_scale = (0.9, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "class PSDataset(Dataset):\n",
    "    def __init__(self, data_dir, landmarks_path):\n",
    "        super().__init__()\n",
    "        self.data_dir = data_dir\n",
    "        self.landmarks_path = landmarks_path\n",
    "        \n",
    "        \n",
    "        self.files = []\n",
    "        self.labels = []\n",
    "        self.landmarks = [] #readjson(landmarks_path)\n",
    "        self.__read_data()\n",
    "    \n",
    "    def __read_data(self):\n",
    "        landmarks = readjson(self.landmarks_path)\n",
    "        for lable, name in enumerate(['real', 'fake']):\n",
    "            sub_dir = f'{self.data_dir}/{name}'\n",
    "            if os.path.exists(sub_dir):\n",
    "                files_sub = get_file_paths(sub_dir)\n",
    "                self.files.extend(files_sub)\n",
    "                self.labels.extend([lable] * len(files_sub))\n",
    "                self.landmarks.extend(self.__get_landmarks(files_sub, landmarks))\n",
    "                \n",
    "    def __get_landmarks(self, files, landmarks):\n",
    "        res = []\n",
    "        for f in files:\n",
    "            res.append(landmarks[get_file_name(f)])\n",
    "        return res   \n",
    "             \n",
    "    def __getitem__(self, index):\n",
    "        return self.files[index], self.labels[index], self.landmarks[index]\n",
    "    \n",
    "    def __len__(self):\n",
    "        return len(self.files)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# def collate_fn(data_batch, mode='train', img_size=(240, 240), crop_scale=0.9):\n",
    "#     trans, mask_trans = get_trans(img_size[0], img_size[1], crop_scale)\n",
    "#     paths, labels, landmarks = data_batch\n",
    "#     imgs = [cv2.imread(p) for p in paths]\n",
    "    \n",
    "#     if mode == 'train':\n",
    "#         faka_image = [self_blend(img, landmarks[i], trans, mask_trans) for i, img in enumerate(imgs)]\n",
    "#         imgs = imgs + faka_image\n",
    "#         labels = torch.concat(labels, torch.ones_like(labels))\n",
    "    \n",
    "#     imgs = torch.tensor(imgs)\n",
    "#     return imgs, labels\n",
    "\n",
    "\n",
    "def get_collate_fn(mode='train', trans=None, mask_trans=None):\n",
    "    \n",
    "    def collate_fn(data_batch):\n",
    "        paths, labels, landmarks = [], [], []\n",
    "        for p, la, lks in data_batch:\n",
    "            paths.append(p)\n",
    "            labels.append(la)\n",
    "            landmarks.append(np.asarray(lks))\n",
    "            \n",
    "        imgs = [cv2.imread(p) for p in paths]\n",
    "        if mode == 'train':\n",
    "            faka_image = [self_blend(img, landmarks[i], trans, mask_trans) \n",
    "                          for i, img in enumerate(imgs)]\n",
    "            imgs = imgs + faka_image\n",
    "            labels = labels + [1] * len(faka_image)\n",
    "        \n",
    "        imgs = torch.tensor(imgs)\n",
    "        labels = torch.tensor(labels, dtype=torch.long)\n",
    "        return imgs, labels\n",
    "        \n",
    "    return collate_fn\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_dataloader(data_dir, landmarks_path, batch_size, img_size, crop_scale, mode='train'):\n",
    "    trans, mask_trans = get_trans(img_size[0], img_size[1], crop_scale)\n",
    "    collate_fn = get_collate_fn(mode, trans, mask_trans)\n",
    "    dataset = PSDataset(data_dir, landmarks_path)\n",
    "    dl = DataLoader(dataset, batch_size, shuffle=True, collate_fn=collate_fn)\n",
    "    return dl"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\albumentations\\augmentations\\transforms.py:1802: FutureWarning: This class has been deprecated. Please use RandomBrightnessContrast\n",
      "  FutureWarning,\n"
     ]
    }
   ],
   "source": [
    "dl = get_dataloader(data_dir, landmarks_path, batch_size, img_size, crop_scale, 'test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(tensor([[[[240, 239, 235],\n",
      "          [237, 236, 232],\n",
      "          [241, 240, 236],\n",
      "          ...,\n",
      "          [152, 153, 149],\n",
      "          [152, 154, 148],\n",
      "          [134, 136, 130]],\n",
      "\n",
      "         [[236, 235, 231],\n",
      "          [236, 235, 231],\n",
      "          [236, 235, 231],\n",
      "          ...,\n",
      "          [150, 151, 147],\n",
      "          [147, 149, 143],\n",
      "          [127, 129, 123]],\n",
      "\n",
      "         [[237, 236, 232],\n",
      "          [238, 237, 233],\n",
      "          [235, 234, 230],\n",
      "          ...,\n",
      "          [138, 139, 135],\n",
      "          [128, 130, 124],\n",
      "          [145, 147, 141]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[ 94,  74,  49],\n",
      "          [119, 100,  73],\n",
      "          [115,  94,  66],\n",
      "          ...,\n",
      "          [125,  99,  63],\n",
      "          [128, 101,  67],\n",
      "          [128, 101,  67]],\n",
      "\n",
      "         [[115,  96,  69],\n",
      "          [101,  82,  55],\n",
      "          [ 73,  52,  24],\n",
      "          ...,\n",
      "          [113,  85,  50],\n",
      "          [120,  92,  58],\n",
      "          [131, 103,  69]],\n",
      "\n",
      "         [[104,  85,  58],\n",
      "          [134, 115,  88],\n",
      "          [105,  84,  56],\n",
      "          ...,\n",
      "          [112,  84,  49],\n",
      "          [113,  85,  51],\n",
      "          [114,  86,  52]]],\n",
      "\n",
      "\n",
      "        [[[250, 250, 250],\n",
      "          [253, 253, 253],\n",
      "          [255, 255, 255],\n",
      "          ...,\n",
      "          [255, 255, 254],\n",
      "          [255, 255, 254],\n",
      "          [254, 255, 253]],\n",
      "\n",
      "         [[251, 251, 251],\n",
      "          [252, 252, 252],\n",
      "          [254, 254, 254],\n",
      "          ...,\n",
      "          [255, 255, 254],\n",
      "          [254, 255, 253],\n",
      "          [254, 255, 253]],\n",
      "\n",
      "         [[254, 254, 254],\n",
      "          [255, 255, 255],\n",
      "          [255, 255, 255],\n",
      "          ...,\n",
      "          [255, 255, 254],\n",
      "          [255, 255, 254],\n",
      "          [255, 255, 254]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[ 50,  93, 156],\n",
      "          [ 50,  93, 156],\n",
      "          [ 45,  89, 152],\n",
      "          ...,\n",
      "          [131, 190, 246],\n",
      "          [137, 196, 252],\n",
      "          [127, 186, 242]],\n",
      "\n",
      "         [[ 34,  78, 139],\n",
      "          [ 45,  89, 150],\n",
      "          [ 31,  74, 137],\n",
      "          ...,\n",
      "          [136, 196, 250],\n",
      "          [142, 202, 255],\n",
      "          [135, 195, 249]],\n",
      "\n",
      "         [[ 38,  82, 143],\n",
      "          [ 47,  91, 152],\n",
      "          [ 37,  80, 143],\n",
      "          ...,\n",
      "          [130, 190, 244],\n",
      "          [129, 189, 243],\n",
      "          [131, 191, 245]]]], dtype=torch.uint8), tensor([0, 0]))\n"
     ]
    }
   ],
   "source": [
    "for i in dl:\n",
    "    print(i)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\albumentations\\augmentations\\transforms.py:1802: FutureWarning: This class has been deprecated. Please use RandomBrightnessContrast\n",
      "  FutureWarning,\n"
     ]
    }
   ],
   "source": [
    "trans, mask_trans = get_trans(img_size[0], img_size[1], crop_scale)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "collate_fn = get_collate_fn('train', trans, mask_trans)\n",
    "collate_fn_test = get_collate_fn('test')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = PSDataset(data_dir, landmarks_path)\n",
    "dl = DataLoader(dataset, shuffle=True, batch_size=batch_size, collate_fn=collate_fn_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(tensor([[[[210, 207, 202],\n",
      "          [205, 202, 197],\n",
      "          [207, 204, 199],\n",
      "          ...,\n",
      "          [200, 198, 190],\n",
      "          [199, 197, 189],\n",
      "          [198, 196, 188]],\n",
      "\n",
      "         [[208, 205, 200],\n",
      "          [206, 203, 198],\n",
      "          [207, 204, 199],\n",
      "          ...,\n",
      "          [198, 196, 188],\n",
      "          [193, 191, 183],\n",
      "          [202, 200, 192]],\n",
      "\n",
      "         [[206, 203, 198],\n",
      "          [205, 202, 197],\n",
      "          [213, 210, 205],\n",
      "          ...,\n",
      "          [204, 202, 194],\n",
      "          [199, 198, 188],\n",
      "          [200, 199, 189]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[131, 115,  56],\n",
      "          [126, 110,  51],\n",
      "          [127, 112,  50],\n",
      "          ...,\n",
      "          [ 83,  72,  12],\n",
      "          [113, 101,  43],\n",
      "          [ 94,  82,  24]],\n",
      "\n",
      "         [[131, 116,  54],\n",
      "          [109,  94,  32],\n",
      "          [120, 105,  43],\n",
      "          ...,\n",
      "          [ 97,  86,  29],\n",
      "          [113, 101,  47],\n",
      "          [ 60,  47,   0]],\n",
      "\n",
      "         [[108,  93,  31],\n",
      "          [115, 100,  38],\n",
      "          [111,  96,  34],\n",
      "          ...,\n",
      "          [ 83,  71,  17],\n",
      "          [ 56,  45,   0],\n",
      "          [ 64,  53,   3]]],\n",
      "\n",
      "\n",
      "        [[[ 90, 108, 125],\n",
      "          [ 90, 108, 125],\n",
      "          [ 91, 108, 127],\n",
      "          ...,\n",
      "          [111, 120, 130],\n",
      "          [116, 125, 135],\n",
      "          [117, 126, 136]],\n",
      "\n",
      "         [[ 85, 103, 120],\n",
      "          [ 86, 104, 121],\n",
      "          [ 90, 107, 126],\n",
      "          ...,\n",
      "          [114, 123, 133],\n",
      "          [119, 128, 138],\n",
      "          [121, 130, 140]],\n",
      "\n",
      "         [[ 88, 106, 123],\n",
      "          [ 90, 108, 125],\n",
      "          [ 93, 110, 129],\n",
      "          ...,\n",
      "          [115, 124, 134],\n",
      "          [119, 128, 138],\n",
      "          [122, 131, 141]],\n",
      "\n",
      "         ...,\n",
      "\n",
      "         [[166, 174, 164],\n",
      "          [166, 174, 164],\n",
      "          [163, 172, 162],\n",
      "          ...,\n",
      "          [158, 170, 164],\n",
      "          [162, 175, 167],\n",
      "          [168, 181, 173]],\n",
      "\n",
      "         [[164, 173, 163],\n",
      "          [161, 170, 160],\n",
      "          [165, 174, 164],\n",
      "          ...,\n",
      "          [162, 175, 167],\n",
      "          [167, 180, 172],\n",
      "          [173, 186, 178]],\n",
      "\n",
      "         [[166, 175, 165],\n",
      "          [166, 175, 165],\n",
      "          [164, 173, 163],\n",
      "          ...,\n",
      "          [159, 172, 164],\n",
      "          [167, 180, 172],\n",
      "          [173, 186, 178]]]], dtype=torch.uint8), tensor([0, 0]))\n"
     ]
    }
   ],
   "source": [
    "for b in dl:\n",
    "    print(b)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('F:/VOS/ubutu-vm/resources/datasets/work/ps dataset/train/real\\\\PhoneT0000001_1.jpg',\n",
       " [[40, 86],\n",
       "  [41, 112],\n",
       "  [46, 138],\n",
       "  [52, 163],\n",
       "  [60, 188],\n",
       "  [74, 209],\n",
       "  [92, 227],\n",
       "  [114, 239],\n",
       "  [141, 242],\n",
       "  [166, 237],\n",
       "  [185, 224],\n",
       "  [201, 206],\n",
       "  [213, 184],\n",
       "  [220, 160],\n",
       "  [225, 135],\n",
       "  [227, 110],\n",
       "  [227, 85],\n",
       "  [56, 66],\n",
       "  [67, 51],\n",
       "  [86, 45],\n",
       "  [106, 45],\n",
       "  [124, 52],\n",
       "  [150, 50],\n",
       "  [168, 43],\n",
       "  [187, 42],\n",
       "  [205, 47],\n",
       "  [215, 62],\n",
       "  [138, 75],\n",
       "  [139, 91],\n",
       "  [140, 107],\n",
       "  [141, 124],\n",
       "  [120, 140],\n",
       "  [130, 142],\n",
       "  [140, 145],\n",
       "  [150, 142],\n",
       "  [159, 139],\n",
       "  [76, 84],\n",
       "  [86, 79],\n",
       "  [99, 78],\n",
       "  [111, 84],\n",
       "  [99, 88],\n",
       "  [87, 88],\n",
       "  [163, 83],\n",
       "  [175, 76],\n",
       "  [187, 76],\n",
       "  [197, 81],\n",
       "  [188, 86],\n",
       "  [175, 86],\n",
       "  [105, 181],\n",
       "  [118, 172],\n",
       "  [131, 166],\n",
       "  [140, 169],\n",
       "  [149, 166],\n",
       "  [161, 171],\n",
       "  [173, 179],\n",
       "  [162, 190],\n",
       "  [151, 194],\n",
       "  [140, 195],\n",
       "  [130, 195],\n",
       "  [118, 191],\n",
       "  [111, 180],\n",
       "  [131, 177],\n",
       "  [140, 177],\n",
       "  [150, 176],\n",
       "  [167, 179],\n",
       "  [150, 180],\n",
       "  [140, 181],\n",
       "  [131, 181],\n",
       "  [67, -2],\n",
       "  [84, -11],\n",
       "  [111, -10],\n",
       "  [141, -11],\n",
       "  [183, -12],\n",
       "  [202, -1],\n",
       "  [226, 43],\n",
       "  [48, 31],\n",
       "  [60, 7],\n",
       "  [38, 83],\n",
       "  [230, 78],\n",
       "  [212, 13],\n",
       "  [173, -9]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img, landmarks = dataset.files[0], dataset.landmarks[0]\n",
    "img, landmarks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "img = cv2.imread(img)\n",
    "landmarks = np.matrix(landmarks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 40,  86],\n",
       "        [ 41, 112],\n",
       "        [ 46, 138],\n",
       "        [ 52, 163],\n",
       "        [ 60, 188],\n",
       "        [ 74, 209],\n",
       "        [ 92, 227],\n",
       "        [114, 239],\n",
       "        [141, 242],\n",
       "        [166, 237],\n",
       "        [185, 224],\n",
       "        [201, 206],\n",
       "        [213, 184],\n",
       "        [220, 160],\n",
       "        [225, 135],\n",
       "        [227, 110],\n",
       "        [227,  85],\n",
       "        [ 56,  66],\n",
       "        [ 67,  51],\n",
       "        [ 86,  45],\n",
       "        [106,  45],\n",
       "        [124,  52],\n",
       "        [150,  50],\n",
       "        [168,  43],\n",
       "        [187,  42],\n",
       "        [205,  47],\n",
       "        [215,  62],\n",
       "        [138,  75],\n",
       "        [139,  91],\n",
       "        [140, 107],\n",
       "        [141, 124],\n",
       "        [120, 140],\n",
       "        [130, 142],\n",
       "        [140, 145],\n",
       "        [150, 142],\n",
       "        [159, 139],\n",
       "        [ 76,  84],\n",
       "        [ 86,  79],\n",
       "        [ 99,  78],\n",
       "        [111,  84],\n",
       "        [ 99,  88],\n",
       "        [ 87,  88],\n",
       "        [163,  83],\n",
       "        [175,  76],\n",
       "        [187,  76],\n",
       "        [197,  81],\n",
       "        [188,  86],\n",
       "        [175,  86],\n",
       "        [105, 181],\n",
       "        [118, 172],\n",
       "        [131, 166],\n",
       "        [140, 169],\n",
       "        [149, 166],\n",
       "        [161, 171],\n",
       "        [173, 179],\n",
       "        [162, 190],\n",
       "        [151, 194],\n",
       "        [140, 195],\n",
       "        [130, 195],\n",
       "        [118, 191],\n",
       "        [111, 180],\n",
       "        [131, 177],\n",
       "        [140, 177],\n",
       "        [150, 176],\n",
       "        [167, 179],\n",
       "        [150, 180],\n",
       "        [140, 181],\n",
       "        [131, 181],\n",
       "        [ 67,  -2],\n",
       "        [ 84, -11],\n",
       "        [111, -10],\n",
       "        [141, -11],\n",
       "        [183, -12],\n",
       "        [202,  -1],\n",
       "        [226,  43],\n",
       "        [ 48,  31],\n",
       "        [ 60,   7],\n",
       "        [ 38,  83],\n",
       "        [230,  78],\n",
       "        [212,  13],\n",
       "        [173,  -9]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "landmarks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "uniform() argument after * must be an iterable, not float",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-62-8c85fe90e87e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mself_blend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlandmarks\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrans\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmask_trans\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32md:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\my_py_toolkit\\cv\\blend_with_landmark.py\u001b[0m in \u001b[0;36mself_blend\u001b[1;34m(img, landmarks, trans, mask_trans)\u001b[0m\n\u001b[0;32m     60\u001b[0m     \u001b[0msource\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     61\u001b[0m     \u001b[0mtarget\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mimg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 62\u001b[1;33m     \u001b[0mmask\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_mask\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlandmarks\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmask_trans\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     63\u001b[0m     \u001b[0mraito\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_raito\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     64\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mtf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtrans\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\my_py_toolkit\\cv\\blend_with_landmark.py\u001b[0m in \u001b[0;36mget_mask\u001b[1;34m(img, landmarks, mask_trans)\u001b[0m\n\u001b[0;32m     26\u001b[0m     \u001b[0mdraw_convex_hull\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmask\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlandmarks\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     27\u001b[0m     \u001b[0mmask\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mmask\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 28\u001b[1;33m     \u001b[0mmask\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmask_trans\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmask\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'image'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     29\u001b[0m     \u001b[1;31m# todo : 高斯核后续测试小不同参数\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     30\u001b[0m     \u001b[0mmask\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGaussianBlur\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmask\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m11\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m11\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\albumentations\\core\\composition.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, force_apply, *args, **data)\u001b[0m\n\u001b[0;32m    208\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    209\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0midx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mt\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtransforms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 210\u001b[1;33m             \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mforce_apply\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mforce_apply\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    211\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    212\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mcheck_each_transform\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\albumentations\\core\\transforms_interface.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, force_apply, *args, **kwargs)\u001b[0m\n\u001b[0;32m     86\u001b[0m                 )\n\u001b[0;32m     87\u001b[0m                 \u001b[0mtargets_as_params\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtargets_as_params\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 88\u001b[1;33m                 \u001b[0mparams_dependent_on_targets\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_params_dependent_on_targets\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtargets_as_params\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     89\u001b[0m                 \u001b[0mparams\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams_dependent_on_targets\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     90\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdeterministic\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Users\\EDY\\anaconda3\\envs\\ps_torch\\lib\\site-packages\\albumentations\\augmentations\\crops\\transforms.py\u001b[0m in \u001b[0;36mget_params_dependent_on_targets\u001b[1;34m(self, params)\u001b[0m\n\u001b[0;32m    342\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    343\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0m_attempt\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 344\u001b[1;33m             \u001b[0mtarget_area\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0muniform\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscale\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0marea\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    345\u001b[0m             \u001b[0mlog_ratio\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mratio\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mratio\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    346\u001b[0m             \u001b[0maspect_ratio\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0muniform\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mlog_ratio\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: uniform() argument after * must be an iterable, not float"
     ]
    }
   ],
   "source": [
    "self_blend(img, landmarks, trans, mask_trans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(('F:/VOS/ubutu-vm/resources/datasets/work/ps dataset/train/real\\\\PhoneT0000023_4.jpg',\n",
       "  'F:/VOS/ubutu-vm/resources/datasets/work/ps dataset/train/real\\\\PhoneT0000008_4.jpg'),\n",
       " tensor([0, 0]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b[0], b[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Test(Dataset):\n",
    "    def __init__(self, data):\n",
    "        self.data = data\n",
    "    def __len__(self):\n",
    "        return len(self.data)\n",
    "    def __getitem__(self, idx):\n",
    "        return self.data[idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = Test(list(range(20)))\n",
    "dl = DataLoader(dataset, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0, 1])\n",
      "tensor([2, 3])\n",
      "tensor([4, 5])\n",
      "tensor([6, 7])\n",
      "tensor([8, 9])\n",
      "tensor([10, 11])\n",
      "tensor([12, 13])\n",
      "tensor([14, 15])\n",
      "tensor([16, 17])\n",
      "tensor([18, 19])\n"
     ]
    }
   ],
   "source": [
    "for batch in dl:\n",
    "    print(batch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 拆分训练测试集\n",
    "par_dir = 'F:/VOS/ubutu-vm/resources/datasets/work'\n",
    "# 台式机\n",
    "# par_dir = 'F:/Sources/datasets/face'\n",
    "data_dir = f'{par_dir}/PhoneTrue100_Std'\n",
    "save_dir = f'{par_dir}/split_train_test'\n",
    "split_raito = 0.9\n",
    "make_path_legal(save_dir)\n",
    "paths = get_file_paths(data_dir)\n",
    "idx_split = int(len(paths) * split_raito)\n",
    "for p in paths[:idx_split]:\n",
    "    train_dir = f'{save_dir}/train'\n",
    "    if not os.path.exists(train_dir):\n",
    "        os.makedirs(train_dir)\n",
    "    with open(p, 'rb') as r:\n",
    "        with open(f'{train_dir}/{get_file_name(p)}', 'wb') as w:\n",
    "            w.write(r.read())\n",
    "for p in paths[idx_split:]:\n",
    "    test_dir = f'{save_dir}/test'\n",
    "    if not os.path.exists(test_dir):\n",
    "        os.makedirs(test_dir)\n",
    "    with open(p, 'rb') as r:\n",
    "        with open(f'{test_dir}/{get_file_name(p)}', 'wb') as w:\n",
    "            w.write(r.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(806, 896)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成假数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'split'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-3a17e53e636a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'split'"
     ]
    }
   ],
   "source": [
    "a = list(range(10))\n",
    "a.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'list' object has no attribute 'split'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-0988a157e505>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'list' object has no attribute 'split'"
     ]
    }
   ],
   "source": [
    "[1, 2].split(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 5, 3, 4])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.randn(2,3,4, 5)\n",
    "a.permute(0, 3, 1, 2).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.73107731, 0.61660078])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.random.randn(2,5)\n",
    "a[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0,  1],\n",
       "         [ 2,  3],\n",
       "         [ 4,  5]],\n",
       "\n",
       "        [[ 6,  7],\n",
       "         [ 8,  9],\n",
       "         [10, 11]],\n",
       "\n",
       "        [[12, 13],\n",
       "         [14, 15],\n",
       "         [16, 17]],\n",
       "\n",
       "        [[18, 19],\n",
       "         [20, 21],\n",
       "         [22, 23]]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(24).reshape(4, 3, 2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([3, 1, 2, 1])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = torch.randint(0, 4, (4,))\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[18, 19],\n",
       "         [20, 21],\n",
       "         [22, 23]],\n",
       "\n",
       "        [[ 6,  7],\n",
       "         [ 8,  9],\n",
       "         [10, 11]],\n",
       "\n",
       "        [[12, 13],\n",
       "         [14, 15],\n",
       "         [16, 17]],\n",
       "\n",
       "        [[ 6,  7],\n",
       "         [ 8,  9],\n",
       "         [10, 11]]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from omegaconf import OmegaConf\n",
    "conf = OmegaConf.create({\"foo\": 10, \"bar\": 20, 123: 456})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 10)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conf['foo'], conf.foo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pytorch_lightning import LightningModule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6850691437721252"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 - 0.31493085622787476"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "01e1d2db26120223f72790c2bb0f23f31be95a9fbbaf387dcc46f865601cfb7f"
  },
  "kernelspec": {
   "display_name": "Python 3.6.5 ('ps_torch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
